package com.jctrip.hotel.source.xxl.atour;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jctrip.hotel.common.constant.XxlJobNameConstant;
import com.jctrip.hotel.common.util.JsonUtil;
import com.jctrip.hotel.domain.dao.HtSourceInfoDao;
import com.jctrip.hotel.domain.entity.HtSourceInfo;
import com.jctrip.hotel.source.model.CommonJobParam;
import com.jctrip.hotel.source.service.atour.AtourTaskService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: zxy
 * @CreateTime: 2023-09-04 20:34
 * @Description:
 */
@Component
@Slf4j
public class SyncAtourLongerRatesPopXxlJob extends IJobHandler {

    @Resource
    private AtourTaskService atourTaskService;
    @Resource
    private HtSourceInfoDao htSourceInfoDao;

    @Override
    @XxlJob(XxlJobNameConstant.SYNC_AUTO_HOTEL_LONGER_RATES_POP)
    public void execute() throws Exception {
        XxlJobHelper.log(XxlJobNameConstant.SYNC_AUTO_HOTEL_LONGER_RATES_POP + " xxl job start");
        try {
            CommonJobParam jobParam = JsonUtil.toObject(CommonJobParam.class, XxlJobHelper.getJobParam());
            HtSourceInfo htSourceInfo =
                    htSourceInfoDao.selectOne(new LambdaQueryWrapper<HtSourceInfo>().eq(HtSourceInfo::getSourceId,
                            jobParam.getSourceId()));
            if (htSourceInfo == null) {
                return;
            }

            atourTaskService.syncAtourLongerRatesTaskFastPop(Integer.valueOf(htSourceInfo.getSourceAppId()),
                    htSourceInfo.getSourceSecret(), (Integer) JsonUtil.toMap(htSourceInfo.getExtra()).get("mebId"),
                    jobParam.getAddDays(), jobParam.getDays());
        } catch (Exception e) {
            log.error(XxlJobNameConstant.SYNC_AUTO_HOTEL_LONGER_RATES_POP + "error", e);
        }
        XxlJobHelper.log(XxlJobNameConstant.SYNC_AUTO_HOTEL_LONGER_RATES_POP + " xxl job end");
    }
}
